import { computed } from 'vue';
import { useAppStore } from '@/store';
import { PermissionUtils } from '@/utils/permission';
import { IUserInfo } from '@/types/interface';

/**
 * 权限管理组合式函数
 */
export function usePermission() {
  const store = useAppStore();

  // 用户权限列表
  const permissions = computed(() => store.state.permissions || []);

  // 用户信息
  const userInfo = computed(() => store.state.user as IUserInfo || {});

  // 菜单列表
  const menus = computed(() => store.state.menus || []);

  // 检查权限
  const hasPermission = (permissionCode: string): boolean => {
    return PermissionUtils.hasPermission(permissionCode);
  };

  // 检查多个权限中的任意一个
  const hasAnyPermission = (permissionCodes: string[]): boolean => {
    return PermissionUtils.hasAnyPermission(permissionCodes);
  };

  // 检查所有权限
  const hasAllPermissions = (permissionCodes: string[]): boolean => {
    return PermissionUtils.hasAllPermissions(permissionCodes);
  };

  // 检查角色
  const hasRole = (roleCode: string): boolean => {
    return PermissionUtils.hasRole(roleCode);
  };

  // 检查多个角色中的任意一个
  const hasAnyRole = (roleCodes: string[]): boolean => {
    return PermissionUtils.hasAnyRole(roleCodes);
  };

  // 检查是否为超级管理员
  const isSuperAdmin = computed(() => PermissionUtils.isSuperAdmin());

  // 检查菜单权限
  const hasMenuPermission = (menuUrl: string): boolean => {
    return PermissionUtils.hasMenuPermission(menuUrl);
  };

  // 获取用户角色列表
  const userRoles = computed(() => PermissionUtils.getUserRoles());

  // 获取用户权限列表
  const userPermissions = computed(() => PermissionUtils.getUserPermissions());

  return {
    // 响应式数据
    permissions,
    userInfo,
    menus,
    isSuperAdmin,
    userRoles,
    userPermissions,

    // 方法
    hasPermission,
    hasAnyPermission,
    hasAllPermissions,
    hasRole,
    hasAnyRole,
    hasMenuPermission
  };
}
